package cn.edu.hpu.jie.action.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckLogin extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;
	private String doFilter;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result = null;
		String str = getDoFilter();
		String[] filterArray = str.split(",");
		String namespace = invocation.getProxy().getNamespace();
		String actionName = invocation.getProxy().getActionName();
		
		if("/".equals(namespace) && (filterArray[0].equals(actionName) || filterArray[1].equals(actionName))) {
			result = invocation.invoke();
		} else {
			invocation.getInvocationContext();
			Map<String, Object> session = ActionContext.getContext().getSession();
			if(session.containsKey("user")) {
				result = invocation.invoke();
			} else {
				result = "index";
			}
		}
		return result;
	}

	//get && set
	public String getDoFilter() {
		return doFilter;
	}

	public void setDoFilter(String doFilter) {
		this.doFilter = doFilter;
	}
	
}
